<template>
  <!--基本信息  -->
  <div>
    <FormPanel
      ref="infoActivityDialog"
      :list="initForm"
      :cols="2"
      formType="add"
      :labelWidth="110"
      :isSubmitBtn="false"
      @formChange="formChangeHandle"
      @formSave="save"
    />
    <!-- 选择车型 -->
    <BaseDialog
      title="选择车型"
      :visible.sync="visibleDialog"
      width="700px"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <carInfoDlg
        ref="carInfoDlg"
        :rowData2="formDataCar"
        @close="closeTypeCarDialog"
      />
    </BaseDialog>

    <div class="dialogBottmView">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button @click="saveDialog" :loading="saveLoading" type="primary">保 存</el-button>
    </div>
  </div>
</template>

<script>
// import typeCar from './infoActivityTypeCar'
// import carInfoDlg from '@/pages/cms-common/product'; //car选择
import { formatdateByDay } from '@/utils';
import carInfoDlg from '@/pages/cms-common/model';
import { dictionary } from '@/utils/dictMixin';
import { saveActivity } from '@/api/cms-api/projectOperating/program/push';
import { saveExcercise } from '@/api/cms-api/projectOperating/program/push';
export default {
  mixins: [dictionary],
  components: {
    // typeCar
    carInfoDlg
  },
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    saveData: {
    }
    formValue: {
    }
    return {
      formDataCar: {},
      fetchParams: {}, //ipa 接口
      v_modelId: '',
      v_modelName: '',
      v_projectItemId: -1 * Math.random() * 100000000000000000,
      v_projectProgramId: -1 * Math.random() * 100000000000000000,
      visibleDialog: false,
      initForm: this.initFormFun(),
      saveLoading:false
    };
  },
  created() {
    if (this.rowData.type === 'edit') {
       var arr = [];
      this.initForm.forEach(x => {
        if (x.fieldName ==  'hdzq') {
          
          arr.push(this.rowData.rowData['hdzq'].split('~')[0]);
          arr.push(this.rowData.rowData['hdzq'].split('~')[1]);
          
          x.initialValue =  arr;
        }else {
           
            x.initialValue = this.rowData.rowData[x.fieldName];
        }
      });
      this.v_modelId  = this.rowData.rowData['modelId'];
      this.v_modelName = this.rowData.rowData['modelName'];
    }
  },
  mounted() {
    // console.log(this.value); //获取到行id 、类型（新增、详情）
    //请求接口进行赋值，自己写吧
  },
  methods: {
    //搜索数据
    initFormFun() {
      return [
        {
          type: 'INPUT',
          label: '项目车型',
          fieldName: 'modelName',
          initialValue: '',
          placeholder: '请选择',
          readonly: true,
          unitRender: () => {
            return <el-button slot="append" icon="el-icon-search" onClick={() => this.openDialog()}></el-button>;
          },
          rules: [
            {
              required: true,
              message: '必填',
              trigger: 'change'
            }
          ]
        },
        {
          type: 'SELECT',
          label: '是否必做',
          fieldName: 'isMustDo',
          placeholder: '请选择',
          initialValue: '',
          itemList: this.createDictList('1004'),
          rules: [
            {
              required: true,
              message: '必填',
              trigger: 'change'
            }
          ]
        },
        { type: 'INPUT', label: '项目地点', fieldName: 'itemAddress', placeholder: '请输入', initialValue: '',
        maxlength:150
         },

        {
          type: 'RANGE_DATE',
          label: '项目周期',
          fieldName: 'hdzq',
          valueFormat: 'yyyy-MM-dd', //返回数据格式
          initialValue: [],
          placeholder: '请选择',
          style: { width: '100%' },
          rules: [
            {
              required: true,
              message: '必填',
              trigger: 'change'
            }
          ]
        },
        {
          type: 'INPUT',
          label: '项目时间',
          fieldName: 'itemDate',
          style: { width: '100%' },
          maxlength:200,
          rules: [
            // {
            //   required: true,
            //   message: '必填',
            //   trigger: 'change'
            // },
            { min: 0, max: 200, message: '长度1~200个字符', trigger: 'blur' }
          ]
        },
        {
          type: 'TEXT_AREA',
          label: '项目名称',
          fieldName: 'itemName',
          rows: 2,
          initialValue: '',
          placeholder: '请输入',
          maxlength:90,
          rules: [{ required: true, message: '必填', trigger: 'blur' }, { min: 1, max: 90, message: '长度1~90个字符', trigger: 'blur' }]
        },
        {
          type: 'TEXT_AREA',
          label: '项目主题',
          fieldName: 'itemTheme',
          rows: 3,
          initialValue: '',
          placeholder: '请输入',
          maxlength:90,
          rules: [{ required: true, message: '必填', trigger: 'blur' }, { min: 1, max: 90, message: '长度1~90个字符', trigger: 'blur' }] //验证
        },
        {
          type: 'TEXT_AREA',
          label: '项目目的',
          fieldName: 'itemPurpose',
          rows: 2,
          initialValue: '',
          placeholder: '请输入',
          maxlength:300,
          rules: [{ required: true, message: '必填', trigger: 'blur' }, { min: 1, max: 300, message: '长度1~300个字符', trigger: 'blur' }] //验证
        },
        {
          type: 'TEXT_AREA',
          label: '项目形式',
          fieldName: 'itemForm',
          rows: 2,
          initialValue: '',
          placeholder: '请输入',
          maxlength:300,
          rules: [{ required: true, message: '必填', trigger: 'blur' }, { min: 1, max: 300, message: '长度1~300个字符', trigger: 'blur' }] //验证
        }
      ];
    },
    openDialog() {
      this.visibleDialog = true;
    },
    closeTypeCarDialog(data) {

      if (data.data != undefined) {
        if (0 == data.data.length) {
          this.$notify.warning({ title: '提示信息', message: '请选择数据!' });
          return;
        }else {

          var modelName = ''
          var modelId = ''
          data.data.forEach(element => {
            if (modelName == '') {
              modelName  = element.modelName; 
              modelId = element.modelId;
            }else {
              modelName += ',' +element.modelName; 
              modelId += ',' + element.modelId;
            }
            });
        }
      }
          
      
   
    

      // debugger
      this.visibleDialog = false; //关闭弹窗
      //返回数据逻辑自己写
      this.fetchParams = { ...this.fetchParams };
      // this.fetchParams.modelId=data.data[0].modelId;
      // console.log('项目下发首页1:', data.data[0].modelName, this.$refs.infoActivityDialog.list[4].fieldName, data.data, this.fetchParams);
     // this.$refs.infoActivityDialog.list[0].initialValue = data.data[0].modelName; //给车型赋值
     if(modelName!=undefined)
        this.$refs.infoActivityDialog.list[0].initialValue = modelName; //给车型赋值

      
      // this.$refs.infoActivityDialog.list[1].initialValue = data.data[0].isMustDo; //
      // this.$refs.infoActivityDialog.list[2].initialValue = data.data[0].itemAddress; //
      // this.$refs.infoActivityDialog.list[3].initialValue = data.data[0].itemStartDate; //
      // this.$refs.infoActivityDialog.list[4].initialValue = data.data[0].modelName; //
      this.v_modelId = modelId;
      this.v_modelName = modelName;
    },
    formChangeHandle(val) {
      this.saveLoading = true;
      this.saveData = val;
      this.saveData.status_zdj = 'add';
      this.saveData.hdzq = this.initForm[3].initialValue[0] + '~' + this.initForm[3].initialValue[1];
      this.saveData.itemEndDate = this.initForm[3].initialValue[1];
      this.saveData.itemStartDate = this.initForm[3].initialValue[0];
      this.saveData.itemDate = this.initForm[4].initialValue;
      this.saveData.projectItemId = '';
      this.saveData.projectProgramId = '';
      this.saveData.modelId = this.v_modelId;
      this.saveData.modelName = this.v_modelName;

      this.$emit('saveData');

      // this.$emit('close', { visible: false });
    },
    formatDates: function(value) {
      // 这里的 value 就是需要过滤的数据
      if (value == null || value == '') {
        return '';
      }
      var date = new Date(value);
      var year = date.getFullYear();
      var month = date.getMonth() + 1;
      var day = date.getDate();
      return year + '-' + month + '-' + day;
    },
    save() {
      this.saveData = {
        // 'dataType':'10461001',
        // 'dataSource':'10451001',

        status_zdj: 'add', //只有添加的数据未来才入库
        isMustDo: this.initForm[1].initialValue,
        itemAddress: this.initForm[2].initialValue,
        hdzq: this.initForm[3].initialValue[0] + '~' + this.initForm[3].initialValue[1],
        itemEndDate: this.initForm[3].initialValue[1],
        itemStartDate: this.initForm[3].initialValue[0],
        itemDate: this.initForm[4].initialValue,
        itemName: this.initForm[5].initialValue,
        itemTheme: this.initForm[6].initialValue,
        itemPurpose: this.initForm[7].initialValue,
        itemForm: this.initForm[8].initialValue,
        projectItemId: '', //this.v_projectItemId, //'1201',//需要填写!
        projectProgramId: '', //this.v_projectProgramId//系统自动生成后,使用主键id再插入其它3张表! projectProgramId
        modelId: this.v_modelId,
        modelName: this.v_modelName
      };
      // console.log("this.savedata1:",this.saveData)
      this.$emit('close', { visible: false, data: this.saveData });
    },
    //保存
    saveDialog() {
  
      this.$refs.infoActivityDialog.SUBMIT_FORM();
    },
    //取消操作
    closeDialog() {
      // this.$emit('close', { visible: false });
      this.$emit('close', this.saveData);
    }
  }
};
</script>
